import { defineStore } from 'pinia'
import axios from 'axios'

export const useUserStore = defineStore('user', {
    state: () => ({
        id: '',
        username: '',
        photo: '',
        rating: '',
        token: '',
        is_login: false,
        pulling_info: true,
        onlineUsers: [], // 在线用户列表
        friends: [], // 好友列表
        pendingRequests: [], // 好友请求列表
        socket: null, // 全局WebSocket连接
    }),
    actions: {
        updateUser(user) {
            this.id = user.id
            this.username = user.username
            this.photo = user.photo
            this.rating = user.rating
            this.is_login = user.is_login
        },
        updateToken(token) {
            this.token = token
        },
        logout() {
            // 关闭WebSocket连接
            if (this.socket) {
                this.socket.close()
                this.socket = null
            }

            this.id = ''
            this.username = ''
            this.photo = ''
            this.token = ''
            this.is_login = false
            this.onlineUsers = []
            this.friends = []
            this.pendingRequests = []
        },
        updatePullingInfo(pulling_info) {
            this.pulling_info = pulling_info
        },
        updateOnlineUsers(users) {
            this.onlineUsers = users // 需要在state中添加onlineUsers字段
        },
        updateFriends(newFriends) {
            this.friends = JSON.parse(JSON.stringify(newFriends)) // 需要在state中添加friends字段
        },
        updatePendingRequests(newRequests) {
            this.pendingRequests = JSON.parse(JSON.stringify(newRequests)) // 需要在state中添加pendingRequests字段
        },

        async login(data) {
            try {
                const resp = await axios.post(
                    'https://www.xujiaojiaojiao.cn/api/user/account/token/',
                    {
                        username: data.username,
                        password: data.password,
                    }
                )
                if (resp.data.code === 1) {
                    localStorage.setItem('jwtToken', resp.data.data.jwtToken)
                    this.updateToken(resp.data.data.jwtToken)
                    data.success()
                } else {
                    data.error(resp.data)
                }
            } catch (error) {
                data.error(error.response?.data || error)
            }
        },

        async getinfo(data) {
            axios
                .get('https://www.xujiaojiaojiao.cn/api/user/account/getInfo/', {
                    headers: {
                        Authorization: 'Bearer ' + this.token,
                    },
                })
                .then((resp) => {
                    if (resp.data.code === 1) {
                        this.updateUser({
                            ...resp.data.data,
                            is_login: true,
                        })
                        data.success()
                    }
                })
                .catch((resp) => {
                    data.error(resp.data)
                    console.log(resp.data)
                })
        },

        fullLogout() {
            localStorage.removeItem('jwtToken')
            this.logout()
        },
    },
})
